package com.yong.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderConsulController {

    public static final String URL = "http://consul-provider-payment";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("consumer/payment/consul")
    public String payment() {
        // 返回对象为响应体中数据转化为对象，可以直接认为是json对象
        String forObject = restTemplate.getForObject(URL + "/payment/consul", String.class);
        return forObject;
    }

    /**
     * 体现了 restTemplate 中采用getForObject和getForEntity的区别
     *
     * @return
     */
    @GetMapping("consumer/payment/consul/entity")
    public String paymentUseEntity() {
        ResponseEntity<String> entity = restTemplate.getForEntity(URL + "/payment/consul", String.class);
        // 获取响应头
        HttpHeaders headers = entity.getHeaders();
        // 获取状态码
        HttpStatus statusCode = entity.getStatusCode();
        // 获取相应体
        String body = entity.getBody();
        log.info("headers: " + headers + "\r\n" + "statusCode: " + statusCode + "\r\n" + "body: " + body);
        return body;
    }
}
